package com.example.administrator.test;


import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class SliderActivity extends AppCompatActivity {
    private ViewPager viewPager1;
    //定义数组存入图片
    int[] mImgId = new int[]{R.mipmap.a, R.mipmap.b,R.mipmap.c};
    //定义集合加载添加图片
    List<ImageView> mList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slider);
        ActionBar bar = getSupportActionBar();
        if(bar!=null){
            bar.hide();
        }



        viewPager1 = (ViewPager)findViewById(R.id.viewPager1);
        //为ViewPager添加动画效果工具类
        viewPager1.setPageTransformer(true, new ZoomOutPageTransformer());
        //绑定Adapter适配器
        viewPager1.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                //获取数组长度
                return mImgId.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                //判断视图是否一样
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //临时集合移除视图
                container.removeView(mList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                //代码创建ImageView控件
                ImageView imageView = new ImageView(SliderActivity.this);
                //ImageView赋值图片
                imageView.setImageResource(mImgId[position]);
                //ImageView赋值动画效果
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                //容器添加ImageView
                container.addView(imageView);
                //临时集合添加ImageView
                mList.add(imageView);
                return imageView;
            }
        });
    }
}
